# -*- coding: utf-8 -*-
"""
__author__ = 'bingo'
__date__ = '2019/3/4'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""


# 生成一个捕获form表单异常的类，form表单校验时候直接继承该类即可直接通过form.get_error属性获取到异常，然后传入前端
class FormMixin():

    @property
    def get_error(self):
        try:
            # 当form实例没有errors时，此处会报错，增加try处理异常
            if hasattr(self, 'errors'):
                errors_dict = self.errors.get_json_data().popitem()[1][0]
                errors_message = errors_dict['message']
                return errors_message
        except Exception as e:
            pass
        return None
